package com.company.cloud.base.modules.app.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.base.modules.api.models.BizCodeModel;
import com.company.cloud.base.modules.base.entity.BizCodeInfo;
import com.company.cloud.base.modules.base.entity.BizTypeInfo;
import com.company.cloud.base.modules.base.service.BizCodeInfoService;
import com.company.cloud.base.modules.base.service.BizTypeInfoService;
import com.company.cloud.base.modules.cache.models.BizTypeModel;
import com.company.cloud.base.modules.cache.service.BizCodeCacheService;
import com.company.cloud.base.modules.mgr.controller.req.BizCodeMgrQueryParam;
import com.company.cloud.base.modules.mgr.controller.req.BizTypeMgrQueryParam;
import com.company.cloud.base.modules.mgr.controller.resp.BizCodeMgrModel;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Login
@RestController
@RequestMapping("/app/base/bizCode")
@Api(tags = "App_Base_业务编码信息")
public class BizTypeCodeAppController {

    @Autowired
    private BizTypeInfoService bizTypeInfoService;
    @Autowired
    private BizCodeInfoService bizCodeInfoService;
    @Autowired
    private BizCodeCacheService bizCodeCacheService;

    @PostMapping("/queryType")
    @ApiOperation(value = "业务分类列表查询", response = BizTypeModel.class)
    public BaseResult query(@RequestBody BizTypeMgrQueryParam params) {
        IPage<BizTypeInfo> data = bizTypeInfoService.page(new Page<>(params.getPage(), params.getRows()),
                new QueryWrapper<BizTypeInfo>()
                        .like(StringUtils.hasText(params.getTypeName()), "type_name", params.getTypeName())
                        .eq(StringUtils.hasText(params.getShowPlatform()), "show_platform", params.getShowPlatform())
        );
        IPage<BizTypeModel> page = data.convert(companyInfo -> {
            BizTypeModel model = new BizTypeModel();
            BeanUtil.copyProperties(companyInfo, model);
            return model;
        });
        return BaseResult.ok(page);
    }

    @PostMapping("/queryCode")
    @ApiOperation(value = "业务信息列表查询", response = BizCodeMgrModel.class)
    public BaseResult query(@RequestBody BizCodeMgrQueryParam params) {
        ValidatorUtils.validate(params);
        String[] codes = params.getTypeCodes().split(StrUtil.COMMA);
        List<String> typeCodes = Arrays.asList(codes);
        IPage<BizCodeInfo> data = bizCodeInfoService.page(new Page<>(params.getPage(), params.getRows()),
                new QueryWrapper<BizCodeInfo>()
                        .in("type_code", typeCodes)
                        .eq(StringUtils.hasText(params.getVname()), "vname", params.getVname())
                        .orderByAsc("weight")
        );
        IPage<BizCodeModel> page = data.convert(companyInfo -> {
            BizCodeModel model = new BizCodeModel();
            BeanUtil.copyProperties(companyInfo, model);
            return model;
        });
        return BaseResult.ok(page);
    }
}
